var http =require('http');
var fs = require('fs');
http.createServer(
    function (req,res) {
        if(req.url=='/'){
            fs.readFile('./titles.json',function (err,data) {
                if(err){
                    console.log(err);
                    res.end('server error');
                }
                else {
                    var titles=JSON.parse(data.toString());
                    fs.readFile('./template.html',function (err,data) {
                        if(err){
                            console.log(err);
                            res.end('server error')
                        }
                        else {
                            var temp=data.toString();
                            var html=temp.replace('%',titles.join('<li></li>'))
                            res.writeHead(200,{'Content-Type':'text/html'})
                            res.end(html);
                        }
                    })
                }
            })
        }
        
    }
).listen(8000,"localhost");